package com.jichangxiu.framework.action;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jichangxiu.common.constant.Constants;
import com.jichangxiu.common.context.JcxContext;
import com.jichangxiu.common.entity.vo.PageResult;
import com.jichangxiu.common.entity.vo.Result;
import com.jichangxiu.common.enums.ErrorType;
import com.jichangxiu.common.utils.ServletUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.util.Date;
import java.util.List;

public class BaseAction {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(DateUtil.parseDate(text));
            }
        });
    }

    protected <T> Result<T> ok() {
        return Result.ok();
    }

    protected <T> Result<T> ok(String message) {
        return Result.ok(message, null);
    }

    protected <T> Result<T> ok(String message, T t) {
        return Result.ok(message, t);
    }

    protected <T> Result<T> ok(T t) {
        return Result.ok(t);
    }

    protected <T> Result<T> no() {
        return Result.no();
    }

    protected <T> Result<T> no(String message) {
        return Result.no(message);
    }

    protected <T> Result<T> no(ErrorType errorType) {
        return Result.no(errorType);
    }

    protected <T> Result<T> result(int rows) {
        return rows > 0 ? ok() : no();
    }

    protected <T> Result<T> result(boolean result) {
        return result ? ok() : no();
    }

    protected PageResult pageResult(Page<?> page) {
        return PageResult.build(page);
    }

    protected PageResult pageResult(Long total, List<?> data) {
        return PageResult.build(total, data);
    }

    protected PageResult pageResult(Page<?> page, List<?> data) {
        return PageResult.build(page, data);
    }

    protected String str(int rows) {
        return rows > 0 ? Constants.CLIENT_MESSAGE_SUCCESS_VALUE : Constants.CLIENT_MESSAGE_FAIL_VALUE;
    }

    protected String str(boolean result) {
        return result ? Constants.CLIENT_MESSAGE_SUCCESS_VALUE : Constants.CLIENT_MESSAGE_FAIL_VALUE;
    }

    protected <T> T getSecurityUser(Class<T> cs) {
        return JcxContext.getSecurityUser(cs);
    }

    protected String getUserId() {
        return JcxContext.getUserId();
    }

    protected String getUsername() {
        return JcxContext.getUserName();
    }

    protected String getDeptId() {
        return JcxContext.getDeptId();
    }

    protected String getTenantId() {
        return JcxContext.getTenantId();
    }

    protected String getJti() {
        return JcxContext.getJti();
    }

    protected String getJwt() {
        return JcxContext.getJwt();
    }

    protected void toDownload(String fileName, byte[] bytes) throws IOException {
        HttpServletResponse httpServletResponse = ServletUtils.getResponse();
        httpServletResponse.reset();
        httpServletResponse.addHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        httpServletResponse.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        httpServletResponse.addHeader("Content-Length", "" + bytes.length);
        httpServletResponse.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(bytes, httpServletResponse.getOutputStream());
    }

}
